package com.m4f.fse.service;

import java.util.Collection;
import java.util.Iterator;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.m4f.fse.components.impl.BaseSearchEngine;

public class ObjetifyEngineService extends BaseService {
	
	public ObjetifyEngineService(Collection<String> classes) throws ClassNotFoundException {
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		for(String className : classes) {
			ObjectifyService.register(classLoader.loadClass(className));
		}
	}
	
	public <T extends BaseSearchEngine> void put(T entity) {
		Objectify ofy = ObjectifyService.begin();
		ofy.put(entity);
	}
	
	public <T extends BaseSearchEngine> void delete(T entity) {
		Objectify ofy = ObjectifyService.begin();
		ofy.delete(entity);
	}
	
	public <T extends BaseSearchEngine> T get(Class<T> clazz, Long id) {
		Objectify ofy = ObjectifyService.begin();
		T entity = ofy.get(clazz, id);
		return entity;
	}
	
	public <T extends BaseSearchEngine> Iterator<T> list(Class<T> clazz) {
		Objectify ofy = ObjectifyService.begin();
		Iterator<T> it = ofy.query(clazz).fetch().iterator();
		return it;
	}
}